package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.dao.ReconSceneMonitorMapper;
import cc.rengu.redp.bizimpl.csmc.domain.ReconSceneMonitor;
import cc.rengu.redp.bizimpl.csmc.service.IReconSceneMonitorService;
import cc.rengu.redp.bizimpl.csmc.vo.ReconSceneMonitorVO;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * @author guohaocong
 */
@Slf4j
@Service
@DS("csmc")
public class ReconSceneMonitorServiceImpl extends ServiceImpl<ReconSceneMonitorMapper, ReconSceneMonitor> implements IReconSceneMonitorService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public IPage<ReconSceneMonitor> findPage(QueryRequest request, ReconSceneMonitorVO reconSceneMonitorVO) {
        try {
            LambdaQueryWrapper<ReconSceneMonitor> lambdaQueryWrapper = new LambdaQueryWrapper();
            if (!StringUtils.isEmpty(reconSceneMonitorVO.getInstId())) {
                lambdaQueryWrapper.eq(ReconSceneMonitor::getInstId, reconSceneMonitorVO.getInstId());
            }
            if (StringUtils.isNotEmpty(reconSceneMonitorVO.getReconSceneId())) {
                lambdaQueryWrapper.like(ReconSceneMonitor::getReconSceneId, reconSceneMonitorVO.getReconSceneId());
            }
            if (!StringUtils.isEmpty(reconSceneMonitorVO.getBatchDate())) {
                lambdaQueryWrapper.eq(ReconSceneMonitor::getBatchDate, reconSceneMonitorVO.getBatchDate());
            }
            Page<ReconSceneMonitor> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, lambdaQueryWrapper);
        } catch (Exception e) {
            log.error("获取信息失败", e);
            return null;
        }
    }

    @Override
    public void editReconSceneMonitor(ReconSceneMonitorVO reconSceneMonitorVO) throws RedpException {
        this.saveData(reconSceneMonitorVO);
    }

    @Override
    public void saveReconSceneMonitor(ReconSceneMonitorVO reconSceneMonitorVO) throws RedpException {
        this.saveData(reconSceneMonitorVO);
    }

    @Override
    public void deleteReconSceneMonitor(String... ids) {
        Arrays.stream(ids).forEach(id -> baseMapper.deleteById(Long.valueOf(id)));
    }


    private void saveData(ReconSceneMonitorVO reconSceneMonitorVO) throws RedpException {
        String id = reconSceneMonitorVO.getId();
        String flag = reconSceneMonitorVO.getFlag();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {   ///修改
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                throw new RedpException("请求参数(id)异常!");
            }
            //根据id查找数据
            ReconSceneMonitor reconSceneMonitor = this.getById(id);
            if (Common.isBlank(reconSceneMonitor)) {
                log.error("根据id没有找到指定数据！!");
                throw new RedpException("根据id没有找到指定数据！");
            }
            BeanUtils.copyProperties(reconSceneMonitorVO, reconSceneMonitor);
            reconSceneMonitor.setBatchDate(lastUpddt);
            this.saveOrUpdate(reconSceneMonitor);
        } else if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {   //新增
            ReconSceneMonitor reconSceneMonitor = new ReconSceneMonitor();
            BeanUtils.copyProperties(reconSceneMonitorVO, reconSceneMonitor);
            reconSceneMonitor.setInstId(user.getInstId());
            reconSceneMonitor.setCreateTime(lastUpddt);
            this.save(reconSceneMonitor);

        }

    }
}
